import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.resps.Tuple;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class RedisDemoZSet {
    public static void main(String[] args) {
        // 1. 连接到 Redis 服务器上.
        JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
        // 2. 从 Redis 连接池中取一个连接出来，并及时释放
        try(Jedis jedis = jedisPool.getResource()){
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
//            test4(jedis);
            test5(jedis);


        }
    }

    private static void test5(Jedis jedis) {
        System.out.println("zrank");
        jedis.flushAll();

        jedis.zadd("key",10,"zhangsan");
        jedis.zadd("key",20,"lisi");
        jedis.zadd("key",30,"wangwu");
        // zrank
        Long rank = jedis.zrank("key", "lisi");
        System.out.println(rank);

    }

    private static void test4(Jedis jedis) {
        System.out.println("zscore");
        jedis.flushAll();

        jedis.zadd("key",10,"zhangsan");
        jedis.zadd("key",20,"lisi");
        jedis.zadd("key",30,"wangwu");
        // zscore
        Double score = jedis.zscore("key", "lisi");
        System.out.println(score);

    }

    private static void test3(Jedis jedis) {
        System.out.println("zrem");
        jedis.flushAll();

        jedis.zadd("key",97.9,"zhangsan");
        Map<String,Double> map = new HashMap<>();
        map.put("lisi",96.5);
        map.put("wangwu",95.5);
        jedis.zadd("key",map);
        // zrem
        long rems = jedis.zrem("key", "zhangsan", "lisi");
        System.out.println(rems);

    }

    private static void test2(Jedis jedis) {
        System.out.println("zcard");
        jedis.flushAll();

        jedis.zadd("key",97.9,"zhangsan");
        Map<String,Double> map = new HashMap<>();
        map.put("lisi",96.5);
        map.put("wangwu",95.5);
        jedis.zadd("key",map);
        // zcard
        long zcard = jedis.zcard("key");
        System.out.println(zcard);


    }

    private static void test1(Jedis jedis) {
        System.out.println("zadd n zrange");
        jedis.flushAll();
        // zadd
        jedis.zadd("key",97.9,"zhangsan");
        Map<String,Double> map = new HashMap<>();
        map.put("lisi",96.5);
        map.put("wangwu",95.5);
        jedis.zadd("key",map);
        // zrange
        List<String> members = jedis.zrange("key", 0, -1);
        System.out.println(members);

        List<Tuple> membersWithScore = jedis.zrangeWithScores("key", 0, -1);
        System.out.println(membersWithScore);

        String member = membersWithScore.get(0).getElement();
        double score = membersWithScore.get(0).getScore();
        System.out.println("member: "+member+", score: "+score);


    }
}
